package com.idconfirmsystem.fragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.idconfirmsystem.R;
import com.idconfirmsystem.adapter.BagAdapter;
import com.idconfirmsystem.lib.NetWorkUtil;
import com.idconfirmsystem.model.Bag;

public class BagQueryFragment extends Fragment implements
		android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener {
	private ListView bagListView;
	private BagAdapter bagAdapter;
	private List<Bag> listBags = new ArrayList<Bag>();

	private CountDownTimer timer;
	ProgressBar progressbar;
	SwipeRefreshLayout swipe;

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_bag, container, false);
		initView(view);
		return view;
	}

	@SuppressLint("InlinedApi")
	private void initView(View view) {
		bagListView = (ListView) view.findViewById(R.id.baglistView);
		progressbar = (ProgressBar) view.findViewById(R.id.progressbar);
		swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
		swipe.setOnRefreshListener(this);
		// 顶部刷新的样式
		swipe.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light,
				android.R.color.holo_blue_bright, android.R.color.holo_orange_light);
		timer = new CountDownTimer(9 * 100, 100) {

			@Override
			public void onTick(long millisUntilFinished) {

				long a = millisUntilFinished / 100;
				if (a == 1) {
					for (int i = 0; i < 25; i++) {
						Bag item = new Bag();
						item.setArrival_time(new Date());
						item.setBags_number("1230012");
						item.setBranch_name("兴业银行" + i);
						item.setBranch_number("xy100" + i);
						item.setBags_seal_no("1222222" + i);
						listBags.add(item);
					}
					bagAdapter = new BagAdapter(getActivity(), bagListView, listBags);
					progressbar.setVisibility(View.GONE);
					bagListView.setAdapter(bagAdapter);
				} else {
					progressbar.setVisibility(View.GONE);
				}
			}

			@Override
			public void onFinish() {

			}
		};
		if (NetWorkUtil.networkCanUse(getActivity())) {
			timer.start();
		} else {
			// progressbar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.net_error_loading));
			// if (sections != null || sections.size() != 0) {
			// adapter = new HomeAdapter(getActivity(), listView);
			// listView.setAdapter(adapter);
			// adapter.resetData(sections);
			// }
		}
	}
}
